package com.ktwlsoft.archivemanage.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ktwlsoft.archivemanage.domain.business.businessService.BusinessFieldService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.OperationRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.BusinessDic;
import com.ktwlsoft.archivemanage.domain.dao.entity.EditHistory;
import com.ktwlsoft.archivemanage.domain.dao.entity.OperationLog;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.businessDic.BusinessDicModifyRequest;
import com.ktwlsoft.archivemanage.payload.businessDic.BusinessDicSaveRequest;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveEditHistoryRequest;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveLogRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 业务字段
 *
 * @author zhuhe on 2019/10/25 15:33
 * @version 1.0
 */
@Api(value = "业务字段", description = "业务字段")
@RestController
@RequestMapping("businessDic")
public class BusinessDicController {

    /**业务字段*/
    private final BusinessFieldService businessFieldService;

    public BusinessDicController(BusinessFieldService businessFieldService) {
        this.businessFieldService = businessFieldService;
    }

    /**
     * 业务字段列表 on 2019/10/24 17:02
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "业务字段列表", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "findPage")
    @UserAuthenticate(actionCode = "FIELDS_PAGE", skip = true)
    public ResponseEntity<?> findPage(@RequestParam(defaultValue = "1") int pn,
                                      @RequestParam(defaultValue = "30") int pageSize,
                                      @ApiParam(name = "dicType", value = "字段类型") @RequestParam(defaultValue = "", required = true) BusinessDic.dicType dicType){
        return ResponseEntity.ok(businessFieldService.findPage(pn,pageSize,dicType));
    }

    /**
     * 业务字段列表 on 2019/10/24 17:02
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "业务字段列表", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "findPageWx")
    public ResponseEntity<?> findPageWx(@RequestParam(defaultValue = "1") int pn,
                                      @RequestParam(defaultValue = "30") int pageSize,
                                      @ApiParam(name = "dicType", value = "字段类型") @RequestParam(defaultValue = "", required = true) BusinessDic.dicType dicType){
        return ResponseEntity.ok(businessFieldService.findPage(pn,pageSize,dicType));
    }

    /**
     * 业务字段新增 on 2019/10/24 17:02
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "业务字段新增", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "save")
    @UserAuthenticate(actionCode = "FIELDS_ADD")
    public ResponseEntity<?> save(@Valid @RequestBody BusinessDicSaveRequest request) {
        Result result = businessFieldService.save(request);
        if(result.getResultType().equals(ResultType.BUSINESS_SUCCESS)){
            saveLog(OperationLog.OperationType.ADD,"添加业务字段《" + request.getDicValue() + "》");
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 业务字段修改 on 2019/10/24 17:02
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "业务字段修改", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "modify")
    @UserAuthenticate(actionCode = "FIELDS_EDIT")
    public ResponseEntity<?> modify(@Valid @RequestBody BusinessDicModifyRequest request) {
        Result result = businessFieldService.modify(request);
        if(result.getResultType().equals(ResultType.BUSINESS_SUCCESS)){
            SaveEditHistoryRequest historyRequest = new SaveEditHistoryRequest();
            historyRequest.setOldEntity(request.getOldValue());
            historyRequest.setNewEntity(request.getNewValue());
            historyRequest.setBusinessId(request.getId());
            historyRequest.setBusinessFrom(EditHistory.BusinessFrom.EDIT_ARCHIVE_TYPE);
            saveLog(result,OperationLog.OperationType.EDIT,"编辑业务字段《" + request.getDicValue() + "》",historyRequest);
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 业务字段删除 on 2019/10/24 17:02
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "业务字段删除", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "deleteById")
    @UserAuthenticate(actionCode = "FIELDS_DELETE")
    public ResponseEntity<?> deleteById(@ApiParam(name = "id", value = "业务字段id") @RequestParam(defaultValue = "", required = true) Long id){
        Result result = businessFieldService.deleteById(id);
        if(result.getResultType().equals(ResultType.BUSINESS_SUCCESS)){
            saveLog(OperationLog.OperationType.DELETE,"删除业务字段《" + result.getData() + "》");
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 业务字段启用禁用 on 2019/10/24 17:02
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "业务字段启用禁用", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "enableById")
    @UserAuthenticate(actionCode = "FIELDS_ENABLE")
    public ResponseEntity<?> enableById(@ApiParam(name = "id", value = "业务字段id") @RequestParam(defaultValue = "", required = true) Long id){
        return ResponseEntity.ok(businessFieldService.enableById(id));
    }

    /**
     * 新增日志 on 2019/11/14 10:37
     *
     * @param operationType 操作类型
     * @param description 描述
     */
    private void saveLog(OperationLog.OperationType operationType, String description){
        OperationRepositoryService.saveLog(new SaveLogRequest(
                operationType,
                description,
                "业务字段",
                OperationLog.BusinessType.SYSTEM
        ));
    }

    /**
     * 新增日志 on 2019/11/14 10:37
     *
     * @param operationType 操作类型
     * @param description 描述
     */
    private void saveLog(Result result,OperationLog.OperationType operationType, String description,SaveEditHistoryRequest editHistoryRequest){
        OperationRepositoryService.saveLog(result,new SaveLogRequest(
                operationType,
                description,
                "业务字段",
                OperationLog.BusinessType.SYSTEM
        ),editHistoryRequest);
    }
}